package com.solarfridge.monitor.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.solarfridge.monitor.shared.DataChart;
import com.solarfridge.monitor.shared.DataChart.DataSet;
import com.solarfridge.monitor.shared.DataChart.DateStampedValue;
import com.solarfridge.monitor.shared.data.Fridge;
import com.solarfridge.monitor.shared.data.FridgeData;

public class FridgeDetailsBinder extends Composite {

	private static FridgeDetailsBinderUiBinder uiBinder = GWT
			.create(FridgeDetailsBinderUiBinder.class);

	interface FridgeDetailsBinderUiBinder extends
			UiBinder<Widget, FridgeDetailsBinder> {
	}

	/**
	 * Create a remote service proxy to talk to the server-side Data service.
	 */
	private final DataServiceAsync mDataService = 
		DataService.Util.getInstance();
	
	private final FridgeListReturner mReturner;
	
	@UiField
	FlexTable detailsTable;
	@UiField
	Label lblHeader;
	@UiField
	VerticalPanel chartPanel;
	
	public FridgeDetailsBinder(Fridge fridge, FridgeListReturner returner) {
		initWidget(uiBinder.createAndBindUi(this));
		mReturner = returner;
		
		lblHeader.setText("Fridge Details for " + fridge.getFridgeId());
		
		
		detailsTable.setText(0, 0, "Id:");
		detailsTable.setText(0, 1, fridge.getFridgeId());
		
		mDataService.getRecentData(fridge.getFridgeId(), 50, new AsyncCallback<List<FridgeData>>() {
			
			@Override
			public void onSuccess(List<FridgeData> result) {
				DateStampedValue[] aTempData = new DateStampedValue[result.size()];
				DateStampedValue[] fTempData = new DateStampedValue[result.size()];
				DateStampedValue[] cTempData = new DateStampedValue[result.size()];
				
				for (int i = 0; i < aTempData.length; i++) {
					FridgeData data = result.get(i);
					aTempData[i] = new DateStampedValue(data.getTimeStamp(), data.getATemp());
					fTempData[i] = new DateStampedValue(data.getTimeStamp(), data.getFTemp());
					cTempData[i] = new DateStampedValue(data.getTimeStamp(), data.getCTemp());
				}
				
				DataSet aTempSet = new DataSet("ATemp", aTempData);
				DataSet fTempSet = new DataSet("FTemp", fTempData);
				DataSet cTempSet = new DataSet("CTemp", cTempData);
				
				DataChart chart = new DataChart(aTempSet, fTempSet, cTempSet);
				chartPanel.add(chart);
				chart.update();	
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());				
			}
		});
			
	}

	@UiHandler("aReturnToList")
	void onAReturnToListClick(ClickEvent event) {
		mReturner.returnToFridgeList();
	}
	
	public interface FridgeListReturner {
		public void returnToFridgeList();
	}

}
